package com.voice.server.command.impl;

import com.alibaba.nls.client.protocol.OutputFormatEnum;
import com.voice.server.command.DeviceCommand;
import com.voice.server.connection.ConnectionInvoke;
import com.voice.server.message.device.FileMessage;
import com.wangjiacai.utils.ali.tts.AliTtsPool;
import com.wangjiacai.utils.ali.tts.service.TtsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
@Slf4j
public class AudioTtlCommand implements DeviceCommand<String> {

    @Autowired
    private ConnectionInvoke connectionInvoke;
    @Autowired
    private AliTtsPool aliTtsPool;
    @Autowired
    private TtsService ttsService;


    @Override
    public void execute(String userId, String deviceKey, String text) {
        try {
            log.info("{}",text);
            ttsService.getBytesByText(aliTtsPool,OutputFormatEnum.MP3,text).whenComplete((bytes, throwable) -> {
                if(throwable==null){
                    FileMessage fileMessage =new FileMessage(bytes);
                    log.info("{}",fileMessage);
                    connectionInvoke.sendDeviceMessage(deviceKey,fileMessage.getTopic(),fileMessage);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
